################################################################################
# Options
################################################################################
set (MATHLIB_DIR "" CACHE PATH "Path to the Mathlib/ultim lib and include directories")
set (GAOL_DIR "" CACHE PATH "Path to the Gaol lib and include directories")

################################################################################
# May be needed to install missing dependencies
################################################################################
include (ExternalProject)
include (IbexUtils)

################################################################################
# Looking for Mathlib/libultim, install it if not found
################################################################################
# Looking for MathLib.h, result is written in MATHLIB_INCDIR
find_header_custom (MATHLIB "MathLib.h" PATHS "${MATHLIB_DIR}" "${GAOL_DIR}")
# Looking for ultim library, result is written in MATHLIB_LIB
find_library_custom (MATHLIB "ultim" PATHS "${MATHLIB_DIR}" "${GAOL_DIR}")

if (MATHLIB_INCDIR AND MATHLIB_LIB)
  get_filename_component (MATHLIB_LIBDIR "${MATHLIB_LIB}" DIRECTORY)
  create_target_import_and_export (ultim ${MATHLIB_LIB} MATHLIB_EXPORTFILE
                                        NAMESPACE Ibex::
                                        INCLUDE_DIRECTORIES ${MATHLIB_INCDIR})
else()
  message (STATUS "Will install and use mathlib from 3rd/ subdirectory")
  if(MINGW)
  ExternalProject_Add (libultim_3rd
                        PREFIX mathlib-2.1.0
                        URL ${CMAKE_CURRENT_SOURCE_DIR}/3rd/mathlib-2.1.0.tar.gz
                        BUILD_IN_SOURCE 1
                        CONFIGURE_COMMAND sh -c "./configure --prefix=<INSTALL_DIR>
                                                      CFLAGS=${CMAKE_C_FLAGS}"
                        LOG_DOWNLOAD 1
                        LOG_CONFIGURE 1
                        LOG_BUILD 1
                        LOG_INSTALL 1
                      )
  else()
  ExternalProject_Add (libultim_3rd
                        PREFIX mathlib-2.1.0
                        URL ${CMAKE_CURRENT_SOURCE_DIR}/3rd/mathlib-2.1.0.tar.gz
                        BUILD_IN_SOURCE 1
                        CONFIGURE_COMMAND ./configure --prefix=<INSTALL_DIR>
                                                      CFLAGS=${CMAKE_C_FLAGS}
                        LOG_DOWNLOAD 1
                        LOG_CONFIGURE 1
                        LOG_BUILD 1
                        LOG_INSTALL 1
                      )
  endif()
  ExternalProject_Get_Property (libultim_3rd INSTALL_DIR)
  set (MATHLIB_LIBDIR "${INSTALL_DIR}/lib")
  set (MATHLIB_INCDIR "${INSTALL_DIR}/include")

  # absolute path to the library
  lib_get_abspath_from_name (MATHLIB_LIB ${MATHLIB_LIBDIR} ultim)

  # this directory needs to exist during generation step to avoid CMake errors
  file (MAKE_DIRECTORY "${MATHLIB_INCDIR}")

  # install headers
  install (DIRECTORY ${MATHLIB_INCDIR}/ COMPONENT devtools
                                    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR_3RD})

  #
  set (_incdir $<BUILD_INTERFACE:${MATHLIB_INCDIR}>$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR_3RD}>)
  create_target_import_and_export (ultim ${MATHLIB_LIB} MATHLIB_EXPORTFILE
                                                INSTALL NAMESPACE Ibex::
                                                INCLUDE_DIRECTORIES ${_incdir}
                                                COMPONENT devtools
                                                DEPENDS libultim_3rd)
  unset (INSTALL_DIR)
endif()

################################################################################
# Looking for Gaol, install it if not found
################################################################################
# Looking for gaol/gaol.h, result is written in GAOL_INCDIR
find_header_custom (GAOL "gaol/gaol.h" PATHS "${GAOL_DIR}")
# Looking for gdtoa library, result is written in GDTOA_LIB
find_library_custom (GDTOA "gdtoa" PATHS "${GAOL_DIR}")
# Looking for gaol library, result is written in GAOL_LIB
find_library_custom (GAOL "gaol" PATHS "${GAOL_DIR}")

# With Gaol, we add "-Wno-deprecated" to remove lots of warnings generated by
# a function definition in gaol_interval.h
CHECK_CXX_COMPILER_FLAG ("-Wno-deprecated" COMPILER_SUPPORTS_WNO_DEPRECATED)
if (COMPILER_SUPPORTS_WNO_DEPRECATED)
  set (gaol_compile_option COMPILE_OPTIONS -Wno-deprecated)
endif()

if (GAOL_INCDIR AND GAOL_LIB AND GDTOA_LIB)
  create_target_import_and_export (gdtoa ${GDTOA_LIB} GDTOA_EXPORTFILE
                                    NAMESPACE Ibex::
                                    ${gaol_compile_option}
                                    INCLUDE_DIRECTORIES ${GAOL_INCDIR})
  create_target_import_and_export (gaol ${GAOL_LIB} GAOL_EXPORTFILE
                                    NAMESPACE Ibex::
                                    ${gaol_compile_option}
                                    INCLUDE_DIRECTORIES ${GAOL_INCDIR}
                                    LINK_LIBRARIES Ibex::gdtoa Ibex::ultim)
else ()
  message (STATUS "Will install and use library gaol from 3rd/ subdirectory")
  if (TARGET libultim_3rd)
    set (_gaol_depends "DEPENDS" "libultim_3rd")
  endif ()
  set (GAOL_CONFIG_ARGS "--disable-preserve-rounding" "--enable-optimize" "--disable-verbose-mode")
  if (WIN32)
    # On windows, we disable SSE instructions (it can generate failures during
    # execution). You can enable SSE instructions by deleting the next line,
    # do it at your own risk.
    set (GAOL_CONFIG_ARGS "${GAOL_CONFIG_ARGS}" "--disable-simd")
  endif ()
  set (GAOL_PATCH "${CMAKE_CURRENT_SOURCE_DIR}/3rd/gaol-4.2.0.all.all.patch")
  if(MINGW)
  ExternalProject_Add (libgaol_3rd
                        PREFIX gaol-4.2.0
                        ${_gaol_depends}
                        URL ${CMAKE_CURRENT_SOURCE_DIR}/3rd/gaol-4.2.0.tar.gz
                        PATCH_COMMAND patch -p1 -i ${GAOL_PATCH}
                        BUILD_IN_SOURCE 1
                        CONFIGURE_COMMAND sh -c "./configure --prefix=<INSTALL_DIR>
                                      CFLAGS=${CMAKE_C_FLAGS}
                                      CXXFLAGS=${CMAKE_CXX_FLAGS}
                                      --with-mathlib-include=${MATHLIB_INCDIR}
                                      --with-mathlib-lib=${MATHLIB_LIBDIR}
                                      ${GAOL_CONFIG_ARGS}"
                        LOG_DOWNLOAD 1
                        LOG_CONFIGURE 1
                        LOG_BUILD 1
                        LOG_INSTALL 1
                      )
  else()
  ExternalProject_Add (libgaol_3rd
                        PREFIX gaol-4.2.0
                        ${_gaol_depends}
                        URL ${CMAKE_CURRENT_SOURCE_DIR}/3rd/gaol-4.2.0.tar.gz
                        PATCH_COMMAND patch -p1 -i ${GAOL_PATCH}
                        BUILD_IN_SOURCE 1
                        CONFIGURE_COMMAND ./configure --prefix=<INSTALL_DIR>
                                      CFLAGS=${CMAKE_C_FLAGS}
                                      CXXFLAGS=${CMAKE_CXX_FLAGS}
                                      --with-mathlib-include=${MATHLIB_INCDIR}
                                      --with-mathlib-lib=${MATHLIB_LIBDIR}
                                      ${GAOL_CONFIG_ARGS}
                        LOG_DOWNLOAD 1
                        LOG_CONFIGURE 1
                        LOG_BUILD 1
                        LOG_INSTALL 1
                      )
  endif()
  ExternalProject_Get_Property (libgaol_3rd INSTALL_DIR)

  set (_incdir ${INSTALL_DIR}/include)

  # absolute path to the library
  lib_get_abspath_from_name (GAOL_LIB ${INSTALL_DIR}/lib gaol)
  lib_get_abspath_from_name (GDTOA_LIB ${INSTALL_DIR}/lib gdtoa)

  # this directory needs to exist during generation step to avoid CMake errors
  file (MAKE_DIRECTORY "${_incdir}")

  # install headers
  install (DIRECTORY ${_incdir}/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR_3RD}
                                 COMPONENT devtools)

  #
  set (_incdir $<BUILD_INTERFACE:${_incdir}>$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR_3RD}>)
  create_target_import_and_export (gdtoa ${GDTOA_LIB} GDTOA_EXPORTFILE INSTALL
                                    NAMESPACE Ibex::
                                    ${gaol_compile_option}
                                    INCLUDE_DIRECTORIES ${_incdir}
                                    COMPONENT devtools
                                    DEPENDS libgaol_3rd)
  create_target_import_and_export (gaol ${GAOL_LIB} GAOL_EXPORTFILE INSTALL
                                    NAMESPACE Ibex::
                                    ${gaol_compile_option}
                                    COMPONENT devtools
                                    INCLUDE_DIRECTORIES ${_incdir}
                                    LINK_LIBRARIES Ibex::gdtoa Ibex::ultim
                                    DEPENDS libgaol_3rd)
  unset (INSTALL_DIR)
endif ()

list (APPEND EXPORTFILES "${MATHLIB_EXPORTFILE}" "${GDTOA_EXPORTFILE}" "${GAOL_EXPORTFILE}")
set (EXPORTFILES "${EXPORTFILES}" PARENT_SCOPE)
